/* -+- file  ./kf2/Exposition/NotifyWindow.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;

namespace Habazie.kf2.Exposition
{
	
	public class NotifyWindow : Gtk.Window
	{
		private string m_Text = null;
		private string m_Desc = null;
		private Gdk.Pixbuf m_Pixbuf = null;
		private Pango.Layout m_TextLayout;
		private Pango.Layout m_DescLayout;
		private int m_width = 0;
		private int m_height = 0;
		
		static readonly int BORDER_WIDTH = 8;
		static readonly int SPACING = 6;
		
		public NotifyWindow() : base (Gtk.WindowType.Popup)
		{
			Name = "gtk-tooltips";
			AppPaintable = true;
			
			Pango.Context context = CreatePangoContext ();
			m_TextLayout = new Pango.Layout (context);
			m_TextLayout.Wrap = Pango.WrapMode.WordChar;
			m_TextLayout.Width = (int) (200 * Pango.Scale.PangoScale);
			//m_TextLayout.
			
			m_DescLayout = new Pango.Layout (context);
			m_DescLayout.Wrap = Pango.WrapMode.WordChar;
			m_DescLayout.Width = (int) (200 * Pango.Scale.PangoScale);
		}
		
		public string Text {
			get { return m_Text; }
			set {
				m_Text = value;
				m_TextLayout.SetText (m_Text);
				Update ();
			}
		}
		
		public string TextMarkup {
			set {
				m_Text = value;
				m_TextLayout.SetMarkup (value);
				Update ();
			}
		}
		
		public string Desc {
			get { return m_Desc; }
			set {
				m_Desc = value;
				m_DescLayout.SetText (m_Desc);
				Update ();
			}
		}
		
		
		public string DescMarkup {
			set {
				m_Desc = value;
				m_DescLayout.SetMarkup (value);
				Update ();
			}
		}
		
		
		public Gdk.Pixbuf Pixbuf {
			get { return m_Pixbuf; }
			set {
				m_Pixbuf = value;
				Update ();
			}
		}
		
		protected override bool OnExposeEvent (Gdk.EventExpose evnt)
		{
			int x = BORDER_WIDTH;
			int y = BORDER_WIDTH;
			
			/* Draw pixbuf */
			if (m_Pixbuf != null) {
				int pix_w = m_Pixbuf.Width;
				int pix_h = m_Pixbuf.Height;
				
				m_Pixbuf.RenderToDrawable (GdkWindow,
									Style.ForegroundGCs[0],
									 0,   0,
									 x,   y,
									pix_w,  pix_h,
									Gdk.RgbDither.None, 0, 0);
				x += pix_w + SPACING;
			}
			
			/* Draw title text */
			if (m_TextLayout != null) {
				GdkWindow.DrawLayout (Style.ForegroundGCs[0],
									x, y, m_TextLayout);
				int w;
				int h;
				m_TextLayout.GetPixelSize (out w, out h);
				y += h + SPACING;
			}
			
			/* Draw title text */
			if (m_DescLayout != null) {
				GdkWindow.DrawLayout (Style.ForegroundGCs[0],
									x, y, m_DescLayout);
			}
			
			GdkWindow.DrawRectangle (Style.ForegroundGCs[0],
										false,
										0, 0,
										m_width, m_height);
			
			return true;
		}
		
		public void ComputeSize (out int w, out int h)
		{
			w = 0;//2 * BORDER_WIDTH;
			h = 0;//2 * BORDER_WIDTH;
			
			int left_h = 0;
			if (m_Pixbuf != null) {
				w += m_Pixbuf.Width + SPACING;
				left_h = m_Pixbuf.Height;
			}
			
			int right_h = 0;
			int upper_w = 0;
			if (m_TextLayout != null) {
			//	int right_w;
			//	int upper_h;
				m_TextLayout.GetPixelSize (out upper_w, out right_h);
			}
			
			int lower_w = 0;
			if (m_DescLayout != null) {
				int _h;
				m_DescLayout.GetPixelSize (out lower_w, out _h);
				right_h += _h + SPACING;
			}
			
			w += Math.Max (upper_w, lower_w);
			h = Math.Max (left_h, right_h);
			
			w += 2 * BORDER_WIDTH + 1;
			h += 2 * BORDER_WIDTH + 1;
		}
			
		
		private void Update ()
		{
			int x, y;
			ComputeSize (out x, out y);
			m_width = x;
			m_height = y;
			Resize (x+1, y+1);
		}
	}
	
}
